package com.execjar;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhuzi
 * 2024/12/18 17:14
 */
@Slf4j
public class ReadPound {
    /**
     * 读取超时时间
     */
    private static int timeOut = 500;

    public static void main7(String[] args) {
        String strPound = "+000000318-\u0002+000000318\u0003";
        Arrays.stream(StringUtils.split(strPound, "+"))
                .map(item -> {
                    return item.trim();
                }).forEach(System.out::println);
    }

    public static void main5(String[] args) {
        ArrayList<byte[]> strings = receiveData(args[0], args[1]);
        for (byte[] string : strings) {
            String str = new String(string, StandardCharsets.UTF_8);
            log.info("读取结果-[{}-{}]", str.trim(), str);
        }
    }

    public static void maina(String[] args) {


        ArrayList<byte[]> strings = receiveData(args[0], args[1]);
        ArrayList<String> list = Lists.<String>newArrayList();
        for (byte[] string : strings) {
            String str = new String(string, StandardCharsets.UTF_8).trim();
            log.info("读取结果-[{}]", str);
            list.add(str);
        }
        log.info("处理数据");
        List<String> collect = list.stream().filter(i -> StringUtils.isNotBlank(i.trim())).map(i -> {
            List<String> split1 = StrUtil.split(i, "+");
            return split1.stream().filter(ia -> StringUtils.isNotBlank(ia) && ia.trim().length() >= 9).map(ib -> ib.trim()).collect(Collectors.toList());
        }).flatMap(List::stream).collect(Collectors.toList());

        for (String s : collect) {
            log.info("处理完毕数据=[{}]", s);
        }


    }

    public static ArrayList<byte[]> receiveData(String ip, String port) {
        Socket socket = null;
        InputStream is = null;
        ArrayList<byte[]> list = Lists.<byte[]>newArrayList();
        try {
            socket = new Socket();
            socket.connect(new InetSocketAddress(ip, Integer.parseInt(port)), timeOut);
            socket.setSoTimeout(timeOut);
            is = socket.getInputStream();
            byte[] buffer = new byte[50]; // 可以根据需要调整缓冲区大小
            int bytesRead;
            // 循环读取数据直到连接关闭或发生异常
            for (int count = 0; count < 10; count++) {
                bytesRead = is.read(buffer);
                // 处理接收到的数据
                byte[] message = Arrays.copyOf(buffer, bytesRead);
                log.info("读取到的数据[{}]", new String(message, StandardCharsets.UTF_8));
                list.add(message);
            }
        } catch (IOException e) {
            e.printStackTrace();
            // 处理连接或读取异常
        } finally {
            // 关闭连接和I/O,释放资源
            closeAll(is, socket);
        }
        return list;
    }

    private static void closeAll(Closeable... closeables) {
        for (Closeable closeable : closeables) {
            if (closeable != null) {
                try {
                    closeable.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
